#!/usr/bin/env python
# -*- coding:utf-8 -*-
import json
import os.path


@staticmethod
def excute(_base_path, target_file, json_data):
    if os.path.exists(target_file):
        os.remove(target_file)
    # 读取样本文件
    # sample_json_file = _base_path+ '\\json\\Website.json'
    sample_handle_file = _base_path+ '\\template\\SampleRoute.txt'
    try:
        # with open(sample_json_file, 'r', encoding='utf-8') as file:
        #     data = json.load(file)
        with open(sample_handle_file, 'r', encoding='utf-8') as txt_file:
            txt_content = txt_file.read()

        # 替换TXT文件中的占位符为JSON文件中对应的值
        for key, value in json_data.items():
            placeholder = key
            txt_content = txt_content.replace(placeholder, str(value))

        # 打印替换后的内容
        print(txt_content)

        if os.path.exists(target_file):
            # 如果文件存在，则删除文件
            os.remove(target_file)
            print(f"文件 {target_file} 已被删除")
        else:
            print(f"文件 {target_file} 不存在")

        with open(target_file, 'w', encoding='utf-8') as output_file:
            output_file.write(txt_content)

    except FileNotFoundError:
        print(f"Error: The file {target_file} does not exist.")
    except UnicodeDecodeError:
        print(f"Error: Could not decode the file {target_file}. Please check the file encoding.")
    except Exception as e:
        print(f"An unexpected error occurred: {e}")

